package com.cmc.cloud.cmclink.doc.rpcservice.impl;

import com.cmc.cloud.cmclink.agt.api.base.DocumentApi;
import com.cmc.cloud.cmclink.agt.api.base.req.document.DocumentAgreementRateInfoReqDTO;
import com.cmc.cloud.cmclink.agt.api.base.resp.document.AgtBaseInfoDTO;
import com.cmc.cloud.cmclink.agt.api.base.resp.document.DocumentAgreementRateInfoRespDTO;
import com.cmc.cloud.cmclink.doc.rpcservice.AgreementService;
import com.cmes.framework.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 协议数据
 *
 * @author 申天青
 * @date 2025/11/07
 **/
@Service
@Validated
@Slf4j
public class AgreementServiceImpl implements AgreementService {
    @Resource
    private DocumentApi documentApi;

    @Override
    public CommonResult<HashMap<String, ArrayList<AgtBaseInfoDTO>>> getAgtBaseInfoByCsmCodes(String csmCodes) {

        HashMap<String, ArrayList<AgtBaseInfoDTO> > result = new HashMap<>(8);
        for (String csmCode : csmCodes.split(",")) {

            try {
                CommonResult<HashMap<String, ArrayList<AgtBaseInfoDTO>>> commonResult = documentApi.getAgtBaseInfoData(csmCode);
                if (commonResult == null || commonResult.getData() == null || commonResult.getCheckedData() == null) {
                }else{
                    for (Map.Entry<String, ArrayList<AgtBaseInfoDTO>> entry : commonResult.getData().entrySet()) {
                        String key = entry.getKey();
                        ArrayList<AgtBaseInfoDTO> newList = entry.getValue();

                        if (result.containsKey(key)) {
                            // 合并ArrayList
                            ArrayList<AgtBaseInfoDTO> existingList = result.get(key);
                            existingList.addAll(newList);
                        } else {
                            // 直接放入
                            result.put(key, newList);
                        }
                    }
                }
            } catch (Exception ignored) {
            }
        }

        return CommonResult.success(result);
    }

    @Override
    public CommonResult<ArrayList<DocumentAgreementRateInfoRespDTO>> getAgreementRateData(ArrayList<DocumentAgreementRateInfoReqDTO> dtos) {
        CommonResult<ArrayList<DocumentAgreementRateInfoRespDTO>> documentAgreementVersionRateData = documentApi.getDocumentAgreementVersionRateData(dtos);
        return documentAgreementVersionRateData;
    }
}
